<template>
  <div>
    <el-form :model="searchForm">
      <el-row :gutter="20">
        <el-col :span="4">
          <el-form-item
            label="姓名:"
            label-width="65px"
          >
            <el-input
              v-model="searchForm.userName"
              autocomplete="off"
            />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item
            label="手机号:"
            label-width="65px"
          >
            <el-input
              v-model="searchForm.mobileNo"
              autocomplete="off"
            />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <div class="grid-content" />
        </el-col>
        <el-col :span="5">
          <el-button
            type="primary"
            @click="dialogFormVisible = false"
          >查 询</el-button>
          <el-button
            type="primary"
            @click="dialogFormVisible = false"
          >清 空</el-button>
          <el-button
            v-if="addUserPermit"
            type="primary"
            @click="showAddUserForm()"
          >新 增</el-button>
        </el-col>
      </el-row>
    </el-form>
    <el-table
      :data="tableData"
      style="width: 100%"
    >
      <el-table-column
        type="index"
        width="50"
      />
      <el-table-column
        prop="subsyNames"
        label="会所"
      />
      <el-table-column
        prop="userName"
        label="姓名"
      />
      <el-table-column
        prop="mobile"
        label="手机号"
      />
      <el-table-column
        prop="sex"
        label="性别"
      />
      <el-table-column
        prop="timeStr"
        label="更新时间"
      />
      <el-table-column
        fixed="right"
        label="操作"
        width="100"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
          >查看</el-button>
          <el-button
            v-if="editUserPermit"
            type="text"
            size="small"
            @click="showEditUserForm(scope.row)"
          >编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :page-size="10"
      :total="total"
      :pager-count="11"
      :current-page="currentPage"
      layout="prev, pager, next"
      @current-change="currentChange"
    />

    <el-dialog
      :visible.sync="dialogFormVisible"
      title="编辑用户"
    >
      <el-form
        ref="userForm"
        :model="form"
        :rules="editUserRules"
      >
        <el-row :gutter="20">
          <el-col :span="10">
            <el-form-item
              :label-width="formLabelWidth"
              prop="lastName"
              label="姓："
            >
              <el-input
                v-model="form.lastName"
                autocomplete="off"
              />
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item
              :label-width="formLabelWidth"
              prop="firstName"
              label="名："
            >
              <el-input
                v-model="form.firstName"
                autocomplete="off"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="10">
            <el-form-item
              :label-width="formLabelWidth"
              prop="userName"
              label="登陆名："
            >
              <el-input
                v-model="form.userName"
                autocomplete="off"
              />
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item
              :label-width="formLabelWidth"
              prop="mobile"
              label="手机号："
            >
              <el-input
                v-model="form.mobile"
                autocomplete="off"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="10">
            <el-form-item
              :label-width="formLabelWidth"
              prop="password"
              label="密码："
            >
              <el-input
                v-model="form.password"
                type="password"
                autocomplete="off"
              />
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item
              :label-width="formLabelWidth"
              prop="subsyCodes"
              label="公司："
            >
              <el-select
                v-model="form.subsyCodes"
                multiple
                collapse-tags
                placeholder="请选择"
              >
                <el-option
                  v-for="item in subsyComboList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>

      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button
          v-show="showAddBtn"
          type="primary"
          @click="addUser"
        >确 定</el-button>
        <el-button
          v-show="showEditBtn"
          type="primary"
          @click="editUser"
        >确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import request from '../../../utils/request'
import store from '../../../store'

export default {
  data() {
    return {
      total: 50,
      pageSize: 10,
      currentPage: 1,
      tableData: null,
      subsyComboList: null,
      searchForm: {
        userName: ''
      },
      form: {
        lastName: '',
        firstName: '',
        password: '',
        mobile: '',
        userName: '',
        interUserCode: '',
        subsyCodes: ''
      },
      dialogFormVisible: false,
      showAddBtn: false,
      showEditBtn: false,
      formLabelWidth: '85px',
      formConfirm: null,
      addUserPermit: false,
      editUserPermit: false,
      defaultUserForm: null,
      defaultSearchForm: null,
      editUserRules: {
        lastName: [{ required: true, message: '请输入姓', trigger: 'blur' }],
        firstName: [{ required: true, message: '请输入名', trigger: 'blur' }],
        userName: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
        mobile: [{ required: true, message: '请输入手机号', trigger: 'blur' }],
        subsyCodes: [{ required: true, message: '请选择公司', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.fetchData()
    if (store.getters.buttons.length > 0) {
      store.getters.buttons.forEach((item, index) => {
        if (item === 'user:create') {
          this.addUserPermit = true
        }
        if (item === 'user:update') {
          this.editUserPermit = true
        }
      })
    }
  },
  methods: {
    showAddUserForm() {
      this.initCombo()
      this.empty()
      this.dialogFormVisible = true
      this.showAddBtn = true
    },
    showEditUserForm(data) {
      this.dialogFormVisible = true
      this.showEditBtn = true
      this.initCombo()
      request({
        url: '/user/getUerInfoByUserCode',
        method: 'get',
        params: {
          userCode: data.interUserCode
        }
      }).then(response => {
        this.listLoading = false
        this.form.interUserCode = data.interUserCode
        this.form.firstName = response.data.userDtl.userFirstName
        this.form.lastName = response.data.userDtl.userLastName
        this.form.userName = response.data.userBase.userName
        this.form.mobile = response.data.userBase.mobNum
      })
    },
    addUser() {
      this.$refs.userForm.validate(valid => {
        if (valid) {
          this.listLoading = true
          request({
            url: '/user/createUser',
            method: 'post',
            data: {
              firstName: this.form.firstName,
              lastName: this.form.lastName,
              mobNum: this.form.mobile,
              userPwd: this.form.password,
              userName: this.form.userName,
              subsyCodes: this.form.subsyCodes
            }
          }).then(response => {
            this.listLoading = false
            this.dialogFormVisible = false
            this.fetchData()
          })
        }
      })
    },
    editUser() {

    },
    currentChange(currentPage) {
      this.currentPage = currentPage
      this.fetchData()
    },
    initCombo() {
      this.listLoading = true
      request({
        url: '/user/getSubsyComboList',
        method: 'get'
      }).then(response => {
        this.subsyComboList = response.data
        this.listLoading = false
      })
    },
    fetchData() {
      this.listLoading = true
      request({
        url: '/user/getInterUserListData',
        method: 'get',
        params: {
          currentPage: this.currentPage,
          pageSize: this.pageSize
        }
      }).then(response => {
        this.tableData = response.data.userList
        this.total = response.data.userCount
        this.listLoading = false
      })
    },
    empty() {
      if (this.$refs.userForm !== undefined) {
        this.$refs.userForm.resetFields() // 移除校验结果并重置字段值
        this.$refs.userForm.clearValidate() // 移除校验结果
      }
    }
  }

}
</script>
<style lang='scss' scoped>
</style>
